package com.changeover.gwt.client.view.impl;

import com.changeover.gwt.client.view.IMainView;
import com.changeover.gwt.shared.model.customer.ICustomer;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MainView extends Composite implements IMainView {

    private final TaskView taskView;
    private Grid tabPanelTable;
    private Integer currentTabIndex = 0;
    private final VerticalPanel tabContentPanel;
    private CustomerDetailsView tabCustomer;
    private final HTML tabTranches;
    private final HTML tabReport;
    private final Hyperlink logoutLink;

    public MainView() {
        DecoratorPanel contentDecorator = new DecoratorPanel();

        initWidget(contentDecorator);
        contentDecorator.setStyleName("view-container");
        
        LayoutPanel layoutPanel = new LayoutPanel();
        layoutPanel.setStyleName("view-panel");
        contentDecorator.setWidget(layoutPanel);
        
//        Label titleLabel = new Label("Tasks page");
//        titleLabel.setStylePrimaryName("table-title");
//        layoutPanel.add(titleLabel);
        
        tabContentPanel = new VerticalPanel();
        tabContentPanel.setStyleName("tab-content-wrapper");
        layoutPanel.add(tabContentPanel);
        
        Button btnNewButton = new Button("New button");
        tabContentPanel.add(btnNewButton);
        
        taskView = new TaskView();
        tabCustomer = new CustomerDetailsView(null);
        tabTranches = new HTML("Here will be tranches...");
        tabTranches.setSize("100%", "100%");
        tabReport = new HTML("Here will be reporting...");
        tabReport.setSize("100%", "100%");
        
        tabPanelTable = new Grid(4, 1);
        tabPanelTable.addStyleName("tab-panel");
        layoutPanel.add(tabPanelTable);
        layoutPanel.setWidgetLeftWidth(tabPanelTable, 0.0, Unit.PX, 100.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(tabPanelTable, 85, Unit.PX, 249.0, Unit.PX);
        
        final Label taskTab = new Label("WorkList");
        tabPanelTable.setWidget(0, 0, taskTab);
                
        final Label customerTab = new Label("Customer");
        tabPanelTable.setWidget(1, 0, customerTab);

        final Label tranchesTab = new Label("Tranches");
        tabPanelTable.setWidget(2, 0, tranchesTab);
        
        final Label reportTab = new Label("Reporting");
        tabPanelTable.setWidget(3, 0, reportTab);
        
        selectTab(currentTabIndex);
        
        taskTab.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                selectTab(0);
            }
        });
        customerTab.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                selectTab(1);
            }
        });
        tranchesTab.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                selectTab(2);
            }
        });
        reportTab.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                selectTab(3);
            }
        });
        
        tabPanelTable.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_MIDDLE);
        tabPanelTable.getCellFormatter().setVerticalAlignment(2, 0, HasVerticalAlignment.ALIGN_MIDDLE);
        tabPanelTable.getCellFormatter().setVerticalAlignment(3, 0, HasVerticalAlignment.ALIGN_MIDDLE);
        
        logoutLink = new Hyperlink("New hyperlink", false, "newHistoryToken");
        logoutLink.setHTML("logout");
        layoutPanel.add(logoutLink);
        layoutPanel.setWidgetLeftWidth(logoutLink, 23.0, Unit.PX, 53.0, Unit.PX);
        layoutPanel.setWidgetTopHeight(logoutLink, 15.0, Unit.PX, 18.0, Unit.PX);
    }
    
    public void setCustomer(ICustomer customer) {
        tabCustomer = new CustomerDetailsView(customer);
        selectTab(1);
    }
    
    private void selectTab(Integer index) {
        clearTabSelection();
        currentTabIndex = index;
        //tabPanelTable.getRowFormatter().getElement(index).getStyle().setBackgroundColor("#BBBBBB");
        tabPanelTable.getRowFormatter().getElement(index).addClassName("tab-active");
        tabContentPanel.clear();
        switch (index) {
        case 0:
            tabContentPanel.add(taskView);
            break;
        case 1:
            tabContentPanel.add(tabCustomer);
            break;
        case 2:
            tabContentPanel.add(tabTranches);
            break;
        case 3:
            tabContentPanel.add(tabReport);
            break;
        default:
            break;
        }
    }
    
    private void clearTabSelection() {
        //tabPanelTable.getRowFormatter().getElement(currentTabIndex).getStyle().setBackgroundColor("#DDDDDD");
        tabPanelTable.getRowFormatter().getElement(currentTabIndex).removeClassName("tab-active");
    }
    
    public TaskView getTaskView() {
        return taskView;
    }

    public Widget asWidget() {
        return this;
    }
    
    @Override
    public HasClickHandlers getLogoutLink() {
        return logoutLink;
    }
}
